//
//  B_ReleaseTopicMainView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/12.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ReleaseTopicMainView.h"
#import "B_ReleaseTopicTitleView.h"
#import "B_ReleaseTopicGoodsTVC.h"
#import "B_ReleaseTopicImageView.h"
#import "B_ReleaseTextViewHeader.h"
#import "B_ReleaseAddGoodsView.h"
#import "B_ReleaseFooterView.h"
#import "B_ReleaseTopicModel.h"
#import "B_SelecetGoodslistModel.h"

@interface B_ReleaseTopicMainView ()<UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic, strong) B_ReleaseFooterView * releaseSure;

@property (nonatomic, strong) B_ReleaseTopicTitleView * section0;

@property (nonatomic, assign) NSInteger number;

@end

@implementation B_ReleaseTopicMainView

- (instancetype)init {
    if (self = [super init]) {
        [self addSubview:self.releaseSure];
        [self.releaseSure mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(self.mas_bottom);
            make.height.equalTo(@(80));
        }];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (80 + 24) : 80;
        [self setTableViewFrameWithTop:1 left:0 right:0 bottom:-bottom];
        [self.tableView registerNib:[UINib nibWithNibName:@"B_ReleaseTopicGoodsTVC" bundle:nil] forCellReuseIdentifier:@"B_ReleaseTopicGoodsTVC"];
        [self setTableview];
    }
    return self;
}

- (void) setTableview {
    WeakSelf(weSelf, self);
    self.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
        return 2;
    };
    self.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
        if (section == 0) {
            return 0;
        } else {
            return weSelf.topicModel.B_GoodsArray.count;
        }
    };
    self.heightForHeaderBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
        if (section == 0) {
            return 60;
        } else {
            return 160;
        }
        return 0.1f;
    };
    self.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
        if (section == 0) {
            B_ReleaseTopicTitleView * section0 = [B_ReleaseTopicTitleView initB_ReleaseTopicTitleView];
            weSelf.section0 = section0;
            section0.B_TitleTextfiled.delegate = weSelf;
            section0.B_TitleNumberLabel.text = [NSString stringWithFormat:@"%lu/20", weSelf.number];
            section0.B_TitleTextfiled.text = weSelf.topicModel.B_TopicTitle;
            return section0;
        } else {
            B_ReleaseTextViewHeader * section1 = [B_ReleaseTextViewHeader initB_ReleaseTextViewHeader];
            section1.B_TextView.placeholder = @"请写文字内容";
            section1.B_TextView.delegate = weSelf;
            section1.B_TextView.text = weSelf.topicModel.B_TopicContent;
            return section1;
        }
        return nil;
    };
    self.heightForFooterBlock = ^CGFloat(UITableView *tableView, NSInteger section) {
        if (section == 0) {
            return 125;
        } else {
            return 60;
        }
        return 0.1f;
    };
    self.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
        if (section == 0) {
            B_ReleaseTopicImageView * section0 = [B_ReleaseTopicImageView initB_ReleaseTopicImageView];
            section0.imageArray = weSelf.topicModel.B_ImageArray;
            section0.collectionTapAction = ^(NSIndexPath *indexPath) {
                //TODO: 判断是不是添加图片
                if (weSelf.collectionClicked) {
                    weSelf.collectionClicked(indexPath);
                }
            };
            section0.deleteImageAction = ^(NSIndexPath *indexPath) {
                if (weSelf.deleteImageAction) {
                    weSelf.deleteImageAction(indexPath);
                }
            };
            
            return section0;
        } else {
            if (weSelf.isBusiness) {        // 是商家,有添加商品的按钮
                B_ReleaseAddGoodsView * section1 = [B_ReleaseAddGoodsView initB_ReleaseAddGoodsView];
                section1.addGoodsAction = ^{
                    if (weSelf.addGoodsAction) {
                        weSelf.addGoodsAction();
                    }
                };
                
                return section1;
            } else {
                return nil;
            }
        }
        return nil;
    };
    self.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
        return 140;
    };
    
    
    self.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
        B_ReleaseTopicGoodsTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_ReleaseTopicGoodsTVC" forIndexPath:indexPath];
        
        B_SelecetGoodslistModel * model = weSelf.topicModel.B_GoodsArray[indexPath.row];
        [cell.B_GoodsImageView sd_setImageWithURL:[NSURL URLWithString:model.goodsImg]];
        cell.B_GoodsName.text = model.goodsName;
        cell.B_GoodsPrice.text = [NSString stringWithFormat:@"¥%@", model.goodsPrice];
        cell.B_GoodsSales.text = [NSString stringWithFormat:@"销量%@", model.goodsSalesVolume];
        cell.B_GoodsRepertory.text = [NSString stringWithFormat:@"库存%@", model.goodsStock];
        if (model.isLongPress) {
            cell.B_LongPressShowView.hidden = NO;
        } else {
            cell.B_LongPressShowView.hidden = YES;
        }
        
        WeakSelf(weCell, cell);
        
        cell.deleteAction = ^{
            //TODO:在这里执行删除
            model.isLongPress = NO;
            [weSelf.topicModel.B_GoodsArray removeObjectAtIndex:indexPath.row];
            [tableView reloadData];
        };
        cell.reuploadAction = ^{
             model.isLongPress = NO;
            if (weSelf.tableviewReuploadClicked) {
                weSelf.tableviewReuploadClicked(indexPath);
            }
        };
        /// 添加长按事件
        [cell addLongPressActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            model.isLongPress = YES;
            weCell.B_LongPressShowView.hidden = NO;
        }];
        return cell;
    };
    
}

- (B_ReleaseFooterView *)releaseSure {
    if (!_releaseSure) {
        WeakSelf(weSelf, self);
        _releaseSure = [B_ReleaseFooterView initB_ReleaseFooterView];
        [_releaseSure.B_SureButton setTitle:@"确认发布" forState:UIControlStateNormal];
        _releaseSure.sureActionBlock = ^{
            if (weSelf.sureAction) {
                weSelf.sureAction();
            }
        };
    }
    return _releaseSure;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    /// TODO: 在这里给textview 的模型赋值
    self.topicModel.B_TopicContent = textView.text;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    /// TODO: 在这里给textfiled 的模型赋值
    self.topicModel.B_TopicTitle = textField.text;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([NSString stringWithFormat:@"%@%@", textField.text, string].length > 20) {
        return NO;
    }
    _number = [NSString stringWithFormat:@"%@%@", textField.text, string].length;
    _section0.B_TitleNumberLabel.text = [NSString stringWithFormat:@"%lu/20", _number];
    return YES;
}

@end
